package com.zyf.chapter01.principle.SOLID.OCP.handler;

import com.zyf.chapter01.principle.SOLID.OCP.AlertRule;
import com.zyf.chapter01.principle.SOLID.OCP.ApiStatInfo;
import com.zyf.chapter01.principle.SOLID.OCP.Notification;
import com.zyf.chapter01.principle.SOLID.OCP.NotificationEmergencyLevel;

/**
 * @author yifeng
 */
public class TpsAlertHandler extends AlertHandler {
    public TpsAlertHandler(AlertRule rule, Notification notification) {
        super(rule, notification);
    }

    @Override
    public void check(ApiStatInfo apiStatInfo) {
        long tps = apiStatInfo.getRequestCount() / apiStatInfo.getDurationOfSeconds();
        // TPS 超过预设最大值，则通知负责人或团队
        if (tps > rule.getMatchedRule(apiStatInfo.getApi()).getMaxTimeoutTps()) {
            notification.notify(NotificationEmergencyLevel.URGENCY, "...");
        }
    }
}
